package eg.backend.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.datanucleus.jpa.annotations.Extension;

import eg.backend.base.BaseRecord;

@MappedSuperclass
public class Record extends BaseRecord {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Temporal(TemporalType.DATE)
	private Date inputDate;
	
	private String category;

	private Set<String> dimensions = new HashSet<String>();

	public String owner;

	//separated by Constants.TAG_SEPARATOR
	private String tags;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Set<String> getDimensions() {
		return dimensions;
	}

	public void setDimensions(Set<String> dimensions) {
		this.dimensions = dimensions;
	}

	public void addDimension(String dimension) {
		String todelete = null;
		Dimension root = null;//dimension.getRootDimension()
		for (String dim : dimensions) {
			Dimension dim1 = new Dimension();//get dimension by key dim
			if (dim1.equals(root)) {
				todelete = dim;
				break;
			}
		}
		if (todelete != null)
			this.dimensions.remove(todelete);
		this.dimensions.add(dimension);
	}

	public Date getInputDate() {
		return inputDate;
	}

	public void setInputDate(Date inputDate) {
		this.inputDate = inputDate;
	}

	public String getTags() {
		return tags;
	}
	
	public void setTags(String tags) {
		this.tags = tags;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	/* common */
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getId()).toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!this.getClass().isInstance(obj)) {
			return false;
		}

		Record record = (Record) obj;
		return new EqualsBuilder().append(this.getId(), record.getId())
				.isEquals();
	}
}
